﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartMotion.Core
{
    public class StationsMgr
    {

        #region 常用功能

        //getStationsInfo
        //AddStation
        //RemoveStation
        //ReplaceStation
        //QueryStation
        //IsStationExist
        //getStationInfo
        //GetAllStationName

        #endregion
        #region 单例

        private static readonly Lazy<StationsMgr> Instance = new Lazy<StationsMgr>(() => new StationsMgr());

        private StationsMgr()
        {
            FilePath = GlobalVars.Ins.GetConfigPath() + "StationMgr.cfg";
        }

        public static StationsMgr Ins { get; } = Instance.Value;

        #endregion 单例

        private ConcurrentDictionary<string, VarInfo> _stations;

        public string FilePath { get; set; }

        public ConcurrentDictionary<string, VarInfo> Stations
        {
            get
            {
                _stations = GlobalVars.Ins.Vars[GlobalVars.StationsMgr];
                return _stations;
            }
        }

        public void Load()
        {
            GlobalVars.Ins.LoadDic(GlobalVars.StationsMgr, FilePath, false);
            foreach (var station in Stations.Values)
            {
                if (!(station.Value is StationBase sb)) continue;
                for (int i = 0; i < sb.AxisesID.Count; i++)
                {
                    sb.Axises.Add(GlobalVars.Ins.GetVar(sb.AxisesID[i]).Value);
                }
            }
        }

        public void Save()
        {
            GlobalVars.Ins.SaveDic(GlobalVars.StationsMgr, FilePath);
        }




    }

}
